package com.qcrud.core;

import com.qcrud.core.paging.Paging;
import com.qcrud.tookit.ConstantPool;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.Map;

@Getter
@Setter
public class SqlData {
    private String statementKey;
    private StatementContext ctx;
    private Class entityClass;
    private SqlMeta sqlMeta;
    private String sql;
    private Map<String, Object> params;

    public SqlData(SqlMeta sqlMeta, String statementKey, Class entityClass) {
        this.sqlMeta = sqlMeta;
        this.statementKey = statementKey;
        this.entityClass = entityClass;
    }

    public SqlData addPosition(int position, Object argument) {
        this.getCtx().getBinding().addPosition(position, argument);
        return this;
    }

    public SqlData addPosition(List<Object> arguments) {
        Binding binding = this.getCtx().getBinding();
        int i = 1;
        for (Object arg : arguments) {
            binding.addPosition(i++, arg);
        }
        return this;
    }

    public String getSql() {
        return null == sql ? sqlMeta.getRawSql() : sql;
    }

    public List<Object> getArgs() {
        List<Object> args = null;
        if (null != params) {
            args = (List<Object>) params.get(ConstantPool.SQL_PARAM_KEY);
        }
        return null == args || args.size() == 0 ? null : args;
    }

    public Paging getPaging() {
        return null == params ? null : (Paging) params.get(ConstantPool.PAGING_KEY);
    }
}
